package com.jt9003.controller;



import com.jt9003.pojo.DrugsExport;
import com.jt9003.pojo.User1;
import com.jt9003.service.DrugsExportService;
import com.jt9003.utils.PageUtils;
import com.jt9003.utils.RedisUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//出库单页面
@RestController
@RequestMapping("/DrugsExportController")
public class DrugsExportController {

    @Resource
    private DrugsExportService drugsExportService;

    @Resource
    private RedisUtil redisUtil;
    //药品出库页面，查询出库单据 未确认的订单
    @RequestMapping("/queryDrugsExport")
    @ResponseBody
    public Object  queryDrugsExport( @RequestParam(value = "documentNo", required = false) String documentNo,
                                     @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                     @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit){
        Map<String , Object> map=new HashMap<String , Object>();
        int start = PageUtils.getStart(page, limit);
        Map<String , Object> param=new HashMap<String , Object>();
        param.put("documentNo",documentNo);
        param.put("start",start);
        param.put("limit",limit);
        System.out.print("documentNo==================================="+documentNo);
        try {
            List<DrugsExport> list = drugsExportService.queryDrugsExport(param);
            Integer count = drugsExportService.queryDrugsExportCount(param);
            if (list != null) {
                map.put("msg", "查询成功");
                map.put("success", true);
                map.put( "count",count);
                map.put("code", 0);
                map.put("data", list);

            } else {
                map.put("message", "查询失败");
                map.put("success", false);
                map.put("result", null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    //添加出库单
    @RequestMapping("/addDrugsExport")
    @ResponseBody
    public  Map<String , Object> addDrugsExport(DrugsExport drugsExport,String token){
        System.out.print("5.4html,token======"+token);
        User1 user1 =(User1) redisUtil.get(token);
        if(user1!=null){
            drugsExport.setEntryPerson(user1.getuName());
        }
        Map<String , Object> map=new HashMap<>();
        try {
           Integer ReturnValue = drugsExportService.addDrugsExport(drugsExport);
            if (ReturnValue==1) {
                map.put("msg", "添加出库单成功");
                map.put("success", true);
            } else {
                map.put("msg", "添加出库单失败");
                map.put("success", false);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    //删除出库单
    @RequestMapping("/delDrugsExport")
    @ResponseBody
    public  Map<String , Object> addDrugsExport(@RequestParam(value="documentNo",required = false) String documentNo){
        Map<String , Object> map=new HashMap<String , Object>();
        try {
            Integer ReturnValue = drugsExportService.deleteDrugsExport(documentNo);
            if (ReturnValue==1) {
                map.put("msg", "删除成功");
                map.put("success", true);
            } else {
                map.put("msg", "删除失败");
                map.put("success", false);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }



























/*
    //药品出库页面，新增出库单 选取入库单出库 根据单据号查询已确认的入库单，不输入单据号，分页显示全部的已确认入库的单据-
    @RequestMapping("/queryWarByDocumentno")
    @ResponseBody
    public  Object  queryWarByDocumentno(@RequestParam(value ="documentNo",required = false) String documentNo,
                                         @RequestParam(value = "currPage",required = false,defaultValue ="1") Integer currPage,
                                         @RequestParam(value = "pageSize",required = false,defaultValue ="5") Integer pageSize){
        Map<String , Object> map=new HashMap<>();
        Map<String , Object> param=new HashMap<>();
        int start = PageUtils.getStart(currPage,pageSize);
        param.put("documentNo",documentNo);
        param.put("start",start);
        param.put("pageSize",pageSize);
        try {
            List<Warehouse> list = drugExportService.queryWarBydocumentno(param);
            System.out.println(list.toString());
            Integer count=drugExportService.countWar(documentNo);
            System.out.println(count);
            map.put("message", "查询成功");
            map.put("success", true);
            map.put("result", list);
            map.put("count",count);
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","查询失败");
            map.put("success",false);
        }
        return map;
    }

    //选中单据时，获取单据上的药品信息
    @RequestMapping("/queryWarAndDrgus")
    @ResponseBody
    public Map<String,Object> queryWarAndDrgus(String drName){
        Map<String , Object> map=new HashMap<>();
        Drugs drugs=drugExportService.queryDrugsByName(drName);
        Warehouse warehouse=drugExportService.queryWarByName(drName);
        if(drugs!=null){
            map.put("message1","查询成功");
            map.put("success1",true);
            map.put("result1",drugs);
        }else{
            map.put("message1","查询失败");
            map.put("success1",false);
            map.put("result1",null);
        }

        if(warehouse!=null){
            map.put("message","查询成功");
            map.put("success",true);
            map.put("result",warehouse);
        }else{
            map.put("message","查询失败");
            map.put("success",false);
            map.put("result",null);
        }
        return map;
    }

    //药品出库页面，查询单据 库房已确认 ，药房未确认的订单，显示到页面上
    @RequestMapping("/queryWarList2")
    @ResponseBody
    public  Map<String , Object> queryWarList2(){
        Map<String , Object> map=new HashMap<>();
       List<Warehouse> list=drugExportService.queryWarList2();
       if(list!=null){
           map.put("message","查询成功");
           map.put("success",true);
           map.put("result",list);
       }else {
           map.put("message","查询失败");
           map.put("success",false);
           map.put("result",null);
       }
    return map;
    }


    @RequestMapping("/queryDrugExport")
    @ResponseBody
    public  Map<String , Object> queryDrugExport(Integer wareId,String  drName){
        Map<String , Object> map=new HashMap<>();
        Drugs drugs =drugExportService.selectDrugsExport(drName);
        Warehouse warehouse=drugExportService.selectWarExport(wareId);
        if(drugs!=null){
            map.put("message1","查询成功");
            map.put("success1",true);
            map.put("result1",drugs);
        }else{
            map.put("message1","查询失败");
            map.put("success1",false);
        }
        if(warehouse!=null){
            map.put("message","查询成功");
            map.put("success",true);
            map.put("result",warehouse);
        }else {
            map.put("message","查询失败");
            map.put("success",false);
        }
        return map;
    }

    //修改出库单 更新数据
    @RequestMapping("/updateDrugExport")
    @ResponseBody
    @Transactional
    public  Map<String,Object> updateDrugExport(Warehouse warehouse,Drugs drugs){
        Map<String , Object> map=new HashMap<>();
        try{
           Integer i =drugExportService.updateDrugs(drugs)+drugExportService.updateWar(warehouse);
            if(i==2){
                map.put("message","修改成功");
                map.put("success",true);
            }else {
                map.put("message","修改失败");
                map.put("success",false);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }catch (Exception e){
        e.printStackTrace();
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }
        return map;
    }

    @RequestMapping("/deleteDrugExport")
    @ResponseBody
    @Transactional
    public Map<String,Object> deleteDrugExport(Integer wareId){
        Map<String , Object> map=new HashMap<>();
        try{
            Integer i=drugExportService.deleteDrugExport(wareId);
            if(i>0){
                map.put("message","删除成功");
                map.put("success",true);
            }else {
                map.put("message","删除失败");
                map.put("success",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return map;
    }*/
}
